
#include "nrf24_device.h"
//low level api for cmd+data writing
//return current value of STATUS reg
static char nrf_cmd_write(char cmd, char* buf, char wrlen)
{
	char status = 0;
	NRF_CSN_LOW();   //Every new command must be started by a high to low transition on CSN
	NRF_DELAY_uS(1); //wait for Data Valid on  MISO 
	status = NRF_SPI_RW(cmd);
	while(wrlen>0)
	{//loop for sending bytes
		NRF_SPI_RW(*buf);
		buf++;
		wrlen--;
	}
	NRF_CSN_HIGH();  //set CSN before return  
	NRF_DELAY_uS(1); //keep CSN high for 1us, CSN Inactive time must no less than 50nS
	return status;
}
//low level api for cmd+data reading
//return current value of STATUS reg
static char nrf_cmd_read(char cmd, char* buf, char rdlen)
{
	char status = 0;
	NRF_CSN_LOW();
	NRF_DELAY_uS(1);
	status = NRF_SPI_RW(cmd);
	while(rdlen>0)
	{//loop for reading bytes
		*buf = NRF_SPI_RW(0xFF);
		buf++;
		rdlen--;
	}
	NRF_CSN_HIGH(); 
	NRF_DELAY_uS(1);
	return status;
}
//low level api for cmd writing
//return current value of STATUS reg
static char nrf_write_cmd_only(char cmd)
{
	return nrf_cmd_write(cmd, (char*)0, 0);
}
/********************************
Descrip:  Read multi-bytes from a register
Arg    :  
          regadr - 5 bit Register Map Address
          buf    - buffer for data saving
          rdlen  - number of bytes to read
Return :  current value of STATUS reg
*********************************/
char nrf_reg_read_buf(char regadr, char* buf, char rdlen)
{
	return nrf_cmd_read(NRF_CMD_R_REG(regadr), buf, rdlen);
}
/********************************
Descrip:  Read byte value from a register
Arg    :  regadr - 5 bit Register Map Address
Return :  the byte read back
*********************************/
char nrf_reg_read_byte(char regadr)
{
	char ret = 0;
	nrf_cmd_read(NRF_CMD_R_REG(regadr), &ret, 1);
	return ret;
}
/********************************
Descrip:  write multi-bytes to a register
Arg    :  
          regadr - 5 bit Register Map Address
          buf    - buffer for data saving
          wrlen  - number of bytes to write
Return :  current value of STATUS reg
*********************************/
char nrf_reg_write_buf(char regadr, char* buf, char wrlen)
{
	return nrf_cmd_write(NRF_CMD_W_REG(regadr), buf, wrlen);
}
/********************************
Descrip:  write byte to a register
Arg    :  
          regadr - 5 bit Register Map Address
          val    - byte to write
Return :  current value of STATUS reg
*********************************/
char nrf_reg_write_byte(char regadr, char val)
{
	return nrf_cmd_write(NRF_CMD_W_REG(regadr), &val, 1);
}
/********************************
Descrip:  flush RX FIFO
Arg    :  none
Return :  current value of STATUS reg
*********************************/     
char nrf_rx_flush(void)
{
	return nrf_write_cmd_only(NRF_CMD_FLUSH_RX);
}
/********************************
Descrip:  read RX-payload width in the RX FIFO.
Arg    :  none
Return :  payload width, used for nrf_rx_read_payload() 
*********************************/ 
char nrf_rx_payload_wid(void)
{
	char wid = 0;
	nrf_cmd_read(NRF_CMD_R_RX_PL_WID, &wid, 1);
	return wid;
}
/********************************
Descrip:  read RX-payload: 1 – 32 bytes. 
Arg    :  buf    - buffer for data saving
          rdlen  - number of bytes to read
Return :  current value of STATUS reg
*********************************/
char nrf_rx_read_payload(char* buf, char rdlen)
{
	return nrf_cmd_read(NRF_CMD_R_RX_PAYLOAD, buf, rdlen);
}
/********************************
Descrip:  flush TX FIFO
Arg    :  none
Return :  current value of STATUS reg
*********************************/
char nrf_tx_flush(void)
{
	return nrf_write_cmd_only(NRF_CMD_FLUSH_TX);
}
/********************************
Descrip:  reuse last transmitted payload.
Arg    :  none
Return :  current value of STATUS reg
*********************************/
char nrf_tx_reuse_payload(void)
{
	return nrf_write_cmd_only(NRF_CMD_REUSE_TX_PL);
}
/********************************
Descrip:  Write TX-payload, 1 – 32 bytes. 
Arg    :  buf    - buffer for data saving
          wrlen  - number of bytes to write
Return :  current value of STATUS reg
*********************************/
char nrf_tx_write_payload(char* buf, char wrlen)
{
	return nrf_cmd_write(NRF_CMD_W_TX_PAYLOAD, buf, wrlen);
}
/********************************
Descrip:  read the STATUS register
Arg    :  none
Return :  current value of STATUS reg
*********************************/
char nrf_status(void)
{
	return nrf_write_cmd_only(NRF_CMD_NOP);
} 
/********************************
Descrip:  write payload to be transmitted together with ACK packet on pipe.
Arg    :  pipe   - valid in the range: NRF_PIPE0 ~ NRF_PIPE5
          buf    - buffer for data saving
          wrlen  - number of bytes to write
Return :  current value of STATUS reg
*********************************/
char nrf_ack_write_payload(char pipe, char* buf, char wrlen)
{
	return nrf_cmd_write(NRF_CMD_W_ACK_PAYLOAD(pipe), buf, wrlen);
}